<template>
  <BasicTable @register="registerTable">
    <template #tableTitle>
      <a-button
        type="primary"
        v-auth="'invoice:add'"
        preIcon="ant-design:plus-outlined"
        @click="handleCreate"
      >
        添加发票</a-button
      >
      <a-button
        type="primary"
        v-auth="'invoice:batchadd'"
        @click="showDrawer"
        style="background: rgb(254, 154, 35); border: none"
        >批量导入</a-button>
      <a-button v-auth="'invoice:export'" preIcon="ant-design:file-excel" @click="onExportXls">导出</a-button>
    </template>

    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--编辑抽屉-->
  <editDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
  <RoleDesc @register="registerDesc"></RoleDesc>
  <a-drawer
    v-model:visible="showFooterTk"
    class="custom-class"
    title="批量导入"
    placement="right"
    width="700px"
    @after-visible-change="afterVisibleChange"
  >
    <div class="scDivClass">
      <div class="divLogo">!</div>
      系统提醒：请通过模板文件批量导入发票
    </div>
    <div class="divOne">
      <a-button class="butOne" target="downloadFile" download href="/template/发票信息导入模板.xls"
        >下载模板文件</a-button
      >
    </div>
    <a-upload
      v-model:file-list="fileList"
      name="file"
      :action="windowInstacnet.config.base_url + '/api/Invoice/import'"
      :headers="headers"
      @change="handleChange"
    >
      <div style="float: left; margin-left: 100px; padding-top: 6px">
        选择文件: <span class="scSpan">请导入模板文件</span>
      </div>
      <a-button style="float: left; margin-top: 5px">
        <upload-outlined></upload-outlined>
        导入
      </a-button>
    </a-upload>
  </a-drawer>
</template>
<script lang="ts" name="doctor" setup>
import { defineComponent, ref, reactive } from "vue";
import { BasicTable, TableAction } from "/@/components/Table";
import { useDrawer } from "/@/components/Drawer";
import { useMessage } from "/@/hooks/web/useMessage";
import { columns, searchFormSchema, tableColumns } from "./index.data";
import { list, del,getExportUrl } from "./index.api";
import { useListPage } from "/@/hooks/system/useListPage";
import editDrawer from "./editDrawer.vue";
import { useSuccess } from "/@/hooks/rw/handleSuccess";
const { rhandleSuccess } = useSuccess();
import { IResultTreeData } from "../../system/dict/dict.type";
import { message } from "ant-design-vue";
import { UploadOutlined } from "@ant-design/icons-vue";
import type { UploadChangeParam } from "ant-design-vue";
import { getToken } from "/@/utils/auth";

const showFooter = ref(true);
const showFooterTk = ref(false);
const [roleUserDrawer, { openDrawer: openRoleUserDrawer }] = useDrawer();
const [registerDrawer, { openDrawer, closeDrawer }] = useDrawer();
const [registerDesc, { openDrawer: openRoleDesc }] = useDrawer();
const { createMessage: msg, createConfirm } = useMessage();
const windowInstacnet: any = window;

// 列表页面公共参数、方法
const { prefixCls, tableContext, onImportXls, onExportXls } = useListPage({
  designScope: "role-template",

  tableProps: {
    api: list,
    columns: columns,
    size: "small",
    pagination: true,
    showTableSetting: true,
    showIndexColumn: true,
    ellipsis: false,
    tableSetting: { fullScreen: true },
    formConfig: {
      schemas: searchFormSchema,
      autoAdvancedCol: 4,
      showAdvancedButton: false,
      baseColProps: { xs: 24, sm: 12, md: 6, lg: 6, xl: 6, xxl: 4 },
      actionColOptions: { xs: 24, sm: 12, md: 6, lg: 6, xl: 6, xxl: 4 },
    },
    actionColumn: {
      width: 90,
    },
    fetchSetting: {
      // 传给后台的当前页字段
      pageField: "pageIndex",
      // 传给后台的每页显示多少条的字段
      sizeField: "pageSize",
      // 接口返回表格数据的字段
      listField: "entities",
      // 接口返回表格总数的字段
    },
  },
  exportConfig: {
    name: '发票信息',
    url: getExportUrl,
  },
});

const visible = ref<boolean>(false);
// const headers = ref<object>(false);

const headers = reactive({
  Authorization: getToken(),
});
console.log(headers);

const afterVisibleChange = (bool: boolean) => {
  console.log("visible", bool);
};

const showDrawer = () => {
  showFooterTk.value = true;
};

const [
  registerTable,
  { reload, setSelectedRowKeys },
  { rowSelection, selectedRowKeys },
] = tableContext;
const handleChange = (info: UploadChangeParam) => {
  console.log(info, "info");

  if (info.file.status !== "uploading") {
    console.log(info.file, info.fileList);
  }
  if (info.file.status === "done") {
    showFooterTk.value = false;
    message.success("批量导入成功");
    reload();
  } else if (info.file.status === "error") {
    let msg = info.file.response.msg;
    message.error(msg || `${info.file.name} 批量导入失败.`);
  }
};

const fileList = ref([]);

console.log(list);

console.log(tableContext);
/**
 * 新增
 */
function handleCreate() {
  showFooter.value = true;
  console.log(openDrawer, "openDrawer");
  openDrawer(true, {
    record: {},
    isUpdate: false,
  });
}
/**
 * 编辑
 */
function handleEdit(record: Recordable) {
  record = JSON.parse(JSON.stringify(record));
  record.toolCategory = record.toolCategoryId;
  showFooter.value = true;

  openDrawer(true, {
    record,
    isUpdate: true,
  });
}
/**
 * 详情
 */
function handleDetail(record) {
  showFooter.value = false;
  openRoleDesc(true, {
    record,
    isUpdate: true,
  });
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  // console.log(record)
  del([record.id]).then((res: IResultTreeData) => {
    if (res.isSucceed) {
      msg.success(res.msg || "删除成功");
    } else {
      msg.error(res.msg || "删除失败");
    }
    reload();
  });
}
/**
 * 批量删除事件
 */

async function handleBatchDelete() {
  createConfirm({
    title: "注意",
    content: `确定要删除这些数据吗？删除后不可还原`,
    iconType: "warning",
    onOk: async () => {
      let id = selectedRowKeys.value;
      const res = await del(id);
      rhandleSuccess(res, reload);
      setSelectedRowKeys([]);
      selectedRowKeys.value = [];
    },
  });
}

/**
 * 角色用户
 */
function handleUser(record) {
  //onSelectChange(selectedRowKeys)
  openRoleUserDrawer(true, record);
}
/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: "编辑",
      auth: "invoice:edit",
      onClick: handleEdit.bind(null, record),
    },
    {
      label: "删除",
      auth: "invoice:delete",
      popConfirm: {
        title: "是否确认删除",
        confirm: handleDelete.bind(null, record),
      },
    },
  ];
}
</script>
<style lang="less" scoped>
.scDivClass {
  font-size: 20px;
  color: #fff;
  display: inline-block;
  width: 100%;
  height: 80px;
  line-height: 80px;
  padding-left: 20px;
  background: rgb(215, 215, 215);
}
.divLogo {
  display: inline-block;
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  border: 2px solid #fff;
  margin-right: 10px;
  border-radius: 50%;
  font-size: 22px;
}
.divOne {
  margin-top: 40px;
  margin-bottom: 200px;
}
.butOne {
  float: right;
}
.scSpan {
  border: 1px solid #ccc;
  color: #ccc;
  font-size: 12px;
  padding: 5px 0;
  display: inline-block;
  width: 300px;
}
/deep/ .ant-upload-list {
  display: none !important;
}
</style>
